package com.kyefer.world2d;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class World2D extends ApplicationAdapter implements InputProcessor {
	private SpriteBatch batch;
	private TextureAtlas textureAtlasLeft;
	private TextureAtlas textureAtlasRight;
	private Animation animationLeft;
	private Animation animationRight;
	private Sprite sprite;
	private TextureRegion leftAnimation;
	private TextureRegion rightAnimation;
	private float elapsedTimeLeft = 0;
	private float elapsedTimeRight = 0;

	@Override
	public void create() {
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();

		batch = new SpriteBatch();
		textureAtlasLeft = new TextureAtlas(Gdx.files.internal("runningboy.atlas"));
		animationLeft = new Animation(1 / 10f, textureAtlasLeft.getRegions());
		textureAtlasRight = new TextureAtlas(Gdx.files.internal("runningboy2.atlas"));
		animationRight = new Animation(1 / 10f, textureAtlasRight.getRegions());
		sprite = new Sprite(textureAtlasLeft.findRegion("boy1"));
		sprite.setPosition(w / 2 - sprite.getWidth() / 2, h / 2 - sprite.getHeight() / 2);

		Gdx.input.setInputProcessor(this);
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		Gdx.graphics.getDeltaTime();

		rightAnimation = animationRight.getKeyFrame(elapsedTimeRight, true);
		leftAnimation = animationLeft.getKeyFrame(elapsedTimeLeft, true);

		if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
			elapsedTimeLeft += Gdx.graphics.getDeltaTime();
			sprite.setRegion(leftAnimation);
			sprite.translateX(-5f);
		} else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
			elapsedTimeRight += Gdx.graphics.getDeltaTime();
			sprite.setRegion(rightAnimation);
			sprite.translateX(5f);
		}
		batch.begin();
		sprite.draw(batch);
		batch.end();
	}

	@Override
	public void dispose() {
		batch.dispose();
		textureAtlasLeft.dispose();
		textureAtlasRight.dispose();
	}

	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		if (sprite.getX() + sprite.getWidth()/2 > screenX){
			sprite.translateX(5f);
		} else
			sprite.translateX(-5f);
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}
}
